﻿using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Storage;
using System.Data.Common;

namespace Loong.EntityFrameworkCore.DbAccessors
{
    public abstract class EfCoreDbAccessorBase<TDbContext>
        where TDbContext : DbContext
    {
        private readonly IDbContextProvider<TDbContext> _dbContextProvider;

        public virtual TDbContext Context => _dbContextProvider.GetDbContext();

        public virtual DbConnection Connection
        {
            get
            {
                return Context.Database.GetDbConnection();
            }
        }

        public virtual DbTransaction Transaction
        {
            get
            {
                return Context.Database.CurrentTransaction?.GetDbTransaction();
            }
        }

        public virtual int? TimeoutSeconds => _dbContextProvider.GetTimeoutSeconds();

        public EfCoreDbAccessorBase(IDbContextProvider<TDbContext> dbContextProvider)
        {
            _dbContextProvider = dbContextProvider;
        }
    }
}
